package ytmc.converter;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import org.apache.commons.lang.StringUtils;
import ytmc.DialogView;
import ytmc.FileUtils;
import ytmc.setup.*;


/**
 *
 * @author Kicok
 */
public class ConverterNewTaskView extends DialogView
{
    private ConverterController controller;


    public ConverterNewTaskView( ConverterController controller )
    {
        this.controller = controller;

        initComponents();

        setLocationRelativeTo( null );

        inputFileField.getDocument().addDocumentListener( new InputFileChangeDocumentListener( controller ) );
    }


    public String getInputFilePath()
    {
        return inputFileField.getText();
    }

    public void setInputFilePath( String path )
    {
        inputFileField.setText( path );
    }

    public String getOutputFilePath()
    {
        return outputFileField.getText();
    }

    public void setOutputFilePath( String path )
    {
        outputFileField.setText( path );
    }

    public boolean isVideoConversion()
    {
        return videoConversionRadioBtn.isSelected();
    }

    public boolean isAudioExtraction()
    {
        return audioExtractionRadioBtn.isSelected();
    }


    private void clearFields()
    {
        setOutputFilePath( "" );
        setInputFilePath( "" );
        videoConversionRadioBtn.setSelected( true );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        conversionTypeBtns = new javax.swing.ButtonGroup();
        videoConversionRadioBtn = new javax.swing.JRadioButton();
        audioExtractionRadioBtn = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        inputFileField = new javax.swing.JTextField();
        showFileDialogBtn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        outputFileField = new javax.swing.JTextField();
        cancelBtn = new javax.swing.JButton();
        addBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nowe zadanie konwersji");
        setResizable(false);

        conversionTypeBtns.add(videoConversionRadioBtn);
        videoConversionRadioBtn.setSelected(true);
        videoConversionRadioBtn.setText("Konwersja wideo");
        videoConversionRadioBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conversionTypeChanged(evt);
            }
        });

        conversionTypeBtns.add(audioExtractionRadioBtn);
        audioExtractionRadioBtn.setText("Ekstrakcja audio");
        audioExtractionRadioBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conversionTypeChanged(evt);
            }
        });

        jLabel1.setText("Film do skonwertowania");

        showFileDialogBtn.setText("...");
        showFileDialogBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showFileDialogBtnActionPerformed(evt);
            }
        });

        jLabel2.setText("Zapisz jako");

        cancelBtn.setText("Anuluj");
        cancelBtn.setDefaultCapable(false);
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        addBtn.setText("Dodaj");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(outputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(videoConversionRadioBtn)
                        .addGap(18, 18, 18)
                        .addComponent(audioExtractionRadioBtn))
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(inputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(showFileDialogBtn))
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(addBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(videoConversionRadioBtn)
                    .addComponent(audioExtractionRadioBtn))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(showFileDialogBtn))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(addBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void showFileDialogBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showFileDialogBtnActionPerformed
    {//GEN-HEADEREND:event_showFileDialogBtnActionPerformed
        JFileChooser inputFileDialog = new JFileChooser();
        inputFileDialog.setAcceptAllFileFilterUsed( false );
        inputFileDialog.setCurrentDirectory( FileSystemView.getFileSystemView().getHomeDirectory() );
        inputFileDialog.setDialogType( JFileChooser.OPEN_DIALOG );
        inputFileDialog.setFileFilter( new VideoExtensionsFileFilter() );

        int result = inputFileDialog.showOpenDialog( this );
        if( result == JFileChooser.APPROVE_OPTION )
        {
            inputFileField.setText( inputFileDialog.getSelectedFile().getAbsolutePath() );
            controller.createOutputFilePath();
        }
    }//GEN-LAST:event_showFileDialogBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBtnActionPerformed
    {//GEN-HEADEREND:event_cancelBtnActionPerformed
        this.setVisible( false );
        clearFields();
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addBtnActionPerformed
    {//GEN-HEADEREND:event_addBtnActionPerformed
        controller.addTask();
        this.setVisible( false );
        clearFields();
    }//GEN-LAST:event_addBtnActionPerformed

    private void conversionTypeChanged(java.awt.event.ActionEvent evt)//GEN-FIRST:event_conversionTypeChanged
    {//GEN-HEADEREND:event_conversionTypeChanged
        controller.createOutputFilePath();
    }//GEN-LAST:event_conversionTypeChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JRadioButton audioExtractionRadioBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.ButtonGroup conversionTypeBtns;
    private javax.swing.JTextField inputFileField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField outputFileField;
    private javax.swing.JButton showFileDialogBtn;
    private javax.swing.JRadioButton videoConversionRadioBtn;
    // End of variables declaration//GEN-END:variables

    private class InputFileChangeDocumentListener implements DocumentListener
    {
        ConverterController controller;

        public InputFileChangeDocumentListener( ConverterController controller )
        {
            this.controller = controller;
        }


        @Override
        public void insertUpdate( DocumentEvent de )
        {
            controller.createOutputFilePath();
        }

        @Override
        public void removeUpdate( DocumentEvent de )
        {
            controller.createOutputFilePath();
        }

        @Override
        public void changedUpdate( DocumentEvent de )
        {
            controller.createOutputFilePath();;
        }
    }

    private class VideoExtensionsFileFilter extends FileFilter
    {
        private Collection<String> videoExtensions;

        public VideoExtensionsFileFilter()
        {
            videoExtensions = Arrays.asList( VideoFormat.friendlyNames() );
        }

        @Override
        public boolean accept( File file )
        {
            if( file.isDirectory() )
            {
                return true;
            }

            String extension = FileUtils.getExtension( file ).toLowerCase();
            return videoExtensions.contains( extension );
        }

        @Override
        public String getDescription()
        {
            return "Pliki wideo (*." + StringUtils.join( videoExtensions, "|*." ) + ")";
        }
    }
}
